package com.bookshare.web.controller;

import com.bookshare.service.base.impl.BookCatalogueService;
import com.bookshare.service.model.BookModel;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by yujia on 16/3/16.
 */
@Controller
@RequestMapping("book/catalogue")
public class BookCatalogueController {
    @Autowired
    private BookCatalogueService bookCatalogueService;

    @RequestMapping("")
    public String toBookCatalogueHome() {
        return "bookcatalogue";
    }

    @RequestMapping("/classification")
    @ResponseBody //没有这个,按页面返回.
    public String initBookClassificationCount() throws IOException {
        Map<String,String> map = bookCatalogueService.queryClassificationAndCount();
        ObjectMapper mapper = new ObjectMapper();
        String jsonlist = mapper.writeValueAsString(map);
        return jsonlist;
    }

    @RequestMapping("/allbook")
    @ResponseBody
    public String initBookCatalogue() throws IOException {
        List<BookModel> list = bookCatalogueService.queryAllBookForCatalogue(1);
        ObjectMapper mapper = new ObjectMapper();
        String jsonlist = mapper.writeValueAsString(list);
        return jsonlist;
    }

    @RequestMapping("/page")
    @ResponseBody
    public String BookPageData(int pageNum) throws IOException {
        List<BookModel> list = bookCatalogueService.queryAllBookForCatalogue(pageNum);
        ObjectMapper mapper = new ObjectMapper();
        String jsonlist = mapper.writeValueAsString(list);
        return jsonlist;
    }

    @RequestMapping("/sumpage")
    @ResponseBody
    public String SumPage() throws IOException {
        String sumPage = bookCatalogueService.querySumPage();
        return sumPage;
    }
}